package com.pic4493.utils;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.util.Log;

import com.ppcodes.imageloader.cache.disc.naming.Md5FileNameGenerator;

public class UtiOffLineManager
{
   private UtiOffLineManager()
   {
	  nameBuilder = new Md5FileNameGenerator();
   }

   private static class OffLineManagerInstance
   {
	  private static final UtiOffLineManager instance = new UtiOffLineManager();
   }

   public static UtiOffLineManager getInstance()
   {
	  return OffLineManagerInstance.instance;
   }

   private ExecutorService offLineDataTaskExecutor;
   private ThreadFactory threadFactory;
   private Md5FileNameGenerator nameBuilder;

   private void initExecutorsIfNeed()
   {
	  if (offLineDataTaskExecutor == null || offLineDataTaskExecutor.isShutdown())
	  {
		 offLineDataTaskExecutor = createExecutor();
	  }
   }

   private ExecutorService createExecutor()
   {
	  threadFactory = new ThreadFactory()
	  {
		 @Override
		 public Thread newThread(Runnable r)
		 {
			Thread t = new Thread(r);
			t.setPriority(Thread.NORM_PRIORITY - 1);
			return t;
		 }
	  };
	  BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
	  return new ThreadPoolExecutor(1, 5, 0L, TimeUnit.MILLISECONDS, taskQueue, threadFactory);
   }

   public String getOffLineData(String nameJson)
   {
	  try
	  {
		 File file = new File(UtiFile.getExternalCacheDir(), nameBuilder.generate(nameJson));
		 return UtiFile.getOffLineData(file);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiOffLineManager_getOffLineData(nameJson) " + e);
	  }
	  return null;
   }
   
   public void saveOffLineData(String nameJson, String content)
   {
	  try
	  {
		 initExecutorsIfNeed();
		 offLineDataTaskExecutor.submit(new SaveOffLineDataTask(nameJson, content));
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiOffLineManager_saveOffLineData(nameJson, content) " + e);
	  }
   }

   public void stopAllSavingTask()
   {
	  if (offLineDataTaskExecutor != null)
	  {
		 try
		 {
			offLineDataTaskExecutor.shutdownNow();
		 }
		 catch (Exception e)
		 {
			Log.d("ERROR", "UtiOffLineManager_stopAllSavingTask() " + e);
		 }
	  }
   }

   private class SaveOffLineDataTask implements Runnable
   {
	  private String nameJson;
	  private String content;
	
	  public SaveOffLineDataTask(String nameJson, String content)
	  {
		 this.nameJson = nameJson;
		 this.content = content;
	  }

	  @Override
	  public void run()
	  {
		 if (UtiFile.hasSdcard())
		 {
			try
			{
			   File file = new File(UtiFile.getExternalCacheDir(), nameBuilder.generate(nameJson));
			   UtiFile.saveOffLineData(file, content);
			}
			catch (Exception e)
			{
			   Log.d("ERROR", "UtiOffLineManager.SaveOffLineDataTask_run() " + e);
			}
		 }
	  }
   }
}
